﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class AfficheInform_Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            this.DataList_DataTable();
            //this.FindAiInfoByUser();
        }
    }

    /// <summary>
    /// 页面加载时查找当前用户应看到的公告通知
    /// </summary>
    protected void FindAiInfoByUser()
    {
        User curLoginUser = (User) Session["user"];
        AfficheInformtbService aiService = new AfficheInformtbService();
        List<AfficheInformtb> list = aiService.findByUser(curLoginUser.Section.SectionId);
        if(list != null)
        {
            AfficheInformInfoSignal aiInfoSignal = AfficheInformInfoSignal.instance();
            if(aiInfoSignal.AiInfos[curLoginUser.LoginName] != null)
            {
                aiInfoSignal.AiInfos.Remove(curLoginUser.LoginName);
            }
            aiInfoSignal.addAiInfos(curLoginUser.LoginName, list);
        }
    }

    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string unfinishUsersid = ((HtmlInputHidden) e.Item.FindControl("HidUnFinishUsersid")).Value;
            User curLoginUser = (User) Session["user"];
            int inde = unfinishUsersid.IndexOf("," + curLoginUser.UserId + ",");
            if(inde == 0 || inde > 0)
            {
                ((Image) e.Item.FindControl("imgNew")).Visible = true;
            }
            string ai_id = ((HtmlInputHidden) e.Item.FindControl("HidAiid")).Value;
            ((HyperLink) e.Item.FindControl("HylAiInfo")).NavigateUrl = "AfficheInformInfo.aspx?ai_id=" + ai_id;
        }
    }

    protected void DataList_DataTable()
    {
        User curLoginUser = (User) Session["user"];
        AfficheInformtbService aiService = new AfficheInformtbService();
        List<AfficheInformtb> list = aiService.findByUser(curLoginUser.Section.SectionId);
        if(list != null)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AfficheInformId", typeof(string));
            dt.Columns.Add("AfficheInformSendUser", typeof(string));
            dt.Columns.Add("AfficheInformTitle", typeof(string));
            dt.Columns.Add("AfficheInformCreateTime", typeof(string));
            dt.Columns.Add("AfficheInformUnFinishUsersid", typeof(string));
            foreach(AfficheInformtb ai in list)
            {
                DataRow dr = dt.NewRow();
                dr["AfficheInformId"] = ai.AfficheInformId.ToString();
                dr["AfficheInformTitle"] = ai.AfficheInformTitle;
                dr["AfficheInformUnFinishUsersid"] = ai.AfficheInformUnFinishUsersid;
                dr["AfficheInformSendUser"] = ai.AfficheInformSendUser.UserName;
                dr["AfficheInformCreateTime"] = ai.AfficheInformCreateTime.ToString();
                dt.Rows.Add(dr);
            }
            this.DataList1.DataSource = dt.DefaultView;
        }
        else
        {
            this.DataList1.DataSource = null;
        }
        this.DataList1.DataBind();
    }
    protected void MyTimer_Tick(object sender, EventArgs e)
    {
        User curLoginUser = (User) Session["user"];
        AfficheInformInfoSignal aiInfoSignal = AfficheInformInfoSignal.instance();
        string script = "";
        if(aiInfoSignal.AiInfos[curLoginUser.LoginName] != null)
        {
            List<AfficheInformtb> list = (List<AfficheInformtb>) aiInfoSignal.AiInfos[curLoginUser.LoginName];
            if(list != null)
            {
                foreach(AfficheInformtb ai in list)
                {
                    script += "window.open('AfficheInformInfoOpen.aspx?ai_id=" + ai.AfficheInformId + "');";
                }
            }
        }
        ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", script, true);
    }
}
